define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Product_info, $ionicSlideBoxDelegate, $stateParams,GoPage,$sce,$timeout,Auth,GoDialog,$ionicScrollDelegate) {
		$scope.goods_id = $stateParams.goods_id;
		$scope.slideIndex = $stateParams.id;//定位到某个tab
		$scope.selectTab = 0;
		$scope.show_discuss = $stateParams.show_discuss;//是否显示“商品评价”，这3个都是字符串类型
		$scope.show_consult = $stateParams.show_consult;//是否显示“商品问答”
		$scope.show_selllog = $stateParams.show_selllog;//是否显示“销售记录”
		$scope.input = new Object();
		$scope.input.comment = "";//提问的内容
		$scope.filter = "";//hasappend：追评； hasimage：有图  
		
//		$scope.slideIndex = 1;//测试
		
		if($scope.show_discuss=="true"||$scope.show_consult=="true"||$scope.show_selllog=="true"){
			$scope.isShowSubheader = true; //显示二级导航
		}
		
		//页面激活
		$scope.$on('$ionicView.afterEnter', function() {
			//滑动切换
			if($scope.slideIndex) {
				$scope.selectTab = $scope.slideIndex;
			};
			GoPage.init_top($scope, 44, 0);
			$scope.init_data();
			$scope.isLogin = Auth.check_loginInfo();//ng-if="consultList.setting.ask=='onlymember'&&isLogin"
		}, false);
		
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		//滑动切换
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};
		
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		//初始化商品评价，页面里要用到该方法
		$scope.init_comments = function(filter){
			if($scope.show_discuss!="true") return;
			$scope.filter = filter;
			Product_info.get_comments($scope.goods_id, $scope.filter, 1, function(data) {
				$scope.comments = data;
				$scope.firstHide[0] = true;
				if(data){
					$scope.avg_num = data.goods_point.avg_num;
				}
//				console.log($scope.comments)
			});
		}
		//初始化商品问答
		function init_consults(){
			if($scope.show_consult!="true") return;
			Product_info.get_consults($scope.goods_id, 1, function(data) {
				$scope.consultList = data;
				$scope.firstHide[1] = true;
//				console.log("问答")
//				console.log(data)
			});
		}
		//初始化销售记录
		function init_selllog(){
			if($scope.show_selllog!="true") return;
			Product_info.get_log($scope.goods_id,1, function(data) {
				$scope.sellList = data;
				$scope.firstHide[2] = true;
//				console.log("销售记录")
//				console.log(data)
			})
		}
		
		//加载数据
		$scope.init_data = function() {
			$scope.firstHide = [false,false,false];//防止一开始就显示没有数据
			//1图文详情
			Product_info.get_description($scope.goods_id, function(data) {
				$scope.description = $sce.trustAsHtml(data);
			})
			//2商品评价
			$scope.init_comments();
			//3商品问答
			init_consults();
			//4、销售记录
			init_selllog();
		};
		
		//查看店家回复--初评
		$scope.showBuyerReply = function(index,add) {
			var data = $scope.comments.discuss_info[index];
			if(add=="add"){//显示追评的店家回复
				if(data.isShowReplyAdd){
					data.isShowReplyAdd = false;
				}else{
					data.isShowReplyAdd = true;
				}
				return;
			}
			//显示初评的店家回复
			if(data.isShowReply){
				data.isShowReply = false;
			}else{
				data.isShowReply = true;
			}
		};
		
		//是否匿名
		$scope.hidden_name = function(){
			$scope.isHiddenName = !$scope.isHiddenName;//true为匿名
		}
		
		//提问
		$scope.submitAsk = function(){
			if(!$scope.input.comment){
				GoDialog.toast("请输入提问的内容！");
				return ;
			}
			Product_info.ask($scope.goods_id,$scope.input.comment, $scope.isHiddenName, function(data) {
				if(data.success){
					$scope.isHiddenName = false;
					$scope.input.comment = "";
					init_consults();//提问完，要重新初始化商品提问
					$ionicScrollDelegate.$getByHandle('consults-handle').scrollTop();
				}
				GoDialog.toast(data.msg);
			})
		}
		
		//查看大图
		$scope.showBigImg = function(imgUrl) {
			GoDialog.showBigImg(imgUrl);
		}
		
		
		/****************上下拉刷新****************/
		$scope.nPage_comment = 1;//评价
		$scope.nPage_consults = 1;//问答
		$scope.nPage_log = 1;//销售记录
		$scope.doRefresh = function(){
			$scope.isLoad = true;
			//商品评价
			if($scope.selectTab == 1){
				Product_info.get_comments($scope.goods_id, $scope.filter, 1, function(data) {
					$scope.comments = data;
					$scope.nPage_comment = 1;
				},function(){
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}
			//商品问答
			if($scope.selectTab == 2){
				Product_info.get_consults($scope.goods_id, 1, function(data) {
					$scope.consultList = data;
					$scope.nPage_consults = 1;
				},function(){
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}
			//销售记录
			if($scope.selectTab == 3){
				Product_info.get_log($scope.goods_id, 1, function(data) {
					$scope.sellList = data;
					$scope.nPage_log = 1;
				},function(){
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}
		};
		
		$scope.doRefreshMore = function(){
			$scope.isLoad = true;
			//商品评价
			if($scope.selectTab == 1){
//				if(!$scope.comments) return;
//				if($scope.nPage_comment>=$scope.comments.page.tPage){
//					$scope.$broadcast('scroll.infiniteScrollComplete');
//					$timeout(function() {
//						$scope.isLoad = false;
//					}, 1500);
//					return;
//				}
				Product_info.get_comments($scope.goods_id, $scope.filter, $scope.nPage_comment+1, function(data) {
				    if(data != null) {
						if($scope.comments.discuss_info != undefined) {
							$scope.comments.discuss_info = $scope.comments.discuss_info.concat(data.discuss_info);
							$scope.nPage_comment += 1;
						} else {
							$scope.comments = data;
						}
					}
				},function(){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}
			//商品问答
			if($scope.selectTab == 2){
				Product_info.get_consults($scope.goods_id,$scope.nPage_consults+1, function(data) {
				    if(data != null) {
						if($scope.consultList.consults != undefined) {
							$scope.consultList.consults = $scope.consultList.consults.concat(data.consults);
							$scope.nPage_consults += 1;
						} else {
							$scope.consultList = data;
						}
					}
				},function(){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}
			//销售记录
			if($scope.selectTab == 3){
				Product_info.get_log($scope.goods_id,$scope.nPage_log+1, function(data) {
				    if(data != null) {
						if($scope.sellList.selllog != undefined) {
							$scope.sellList.selllog = $scope.sellList.selllog.concat(data.selllog);
							$scope.nPage_log += 1;
						} else {
							$scope.sellList = data;
						}
					}
				},function(){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}
		}
		/****************上下拉刷新****************/


	}

	controller.$inject = ['$scope', 'Product_info', '$ionicSlideBoxDelegate', '$stateParams','GoPage','$sce','$timeout','Auth','GoDialog','$ionicScrollDelegate'];

	return app.registerController("product_infoctrl",controller);
});